package com.zwg.tingshu.account.receiver;

import com.zwg.tingshu.account.service.UserAccountService;
import com.zwg.tingshu.common.constant.RocketMqConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author: zwg
 * @date: 2024/11/26
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = RocketMqConstant.QUEUE_USER_REGISTER,
        selectorExpression = "*",
        consumerGroup = "zwg",
        consumeMode = ConsumeMode.ORDERLY,
        maxReconsumeTimes = 3

)
public class AccountReceiver implements RocketMQListener<String> {
    @Autowired
    private UserAccountService userAccountService;

    /**
     * 监听用户注册消息；初始化账户（余额）记录
     * 考虑问题：1、是否需要幂等性处理 2、业务中是否需要事务管理
     * 幂等性：同一个消息被多次投递（生产者重发，服务器重发-网络原因），业务执行一次跟多次结果一样
     *
     * @param message
     */
    @Override
    public void onMessage(String message) {
        log.info("[账户服务]监听用户注册成功消息：{}", message);
        Long userId = Long.valueOf(message);
        userAccountService.saveUserAccount(userId);
    }
}